package com.example.himalaya.adapters;

import android.nfc.Tag;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.himalaya.R;
import com.example.himalaya.utils.LogUtil;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class PlayerTrackViewPagerAdapter extends PagerAdapter {
    private List<Track> mTracks = new ArrayList<>();

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View itemView = LayoutInflater.from(container.getContext()).inflate(R.layout.item_track_view_pager, container, false);
        container.addView(itemView);
        ImageView trackPagerIv = itemView.findViewById(R.id.track_view_pager_item);
        if (trackPagerIv != null) {
            Track track = mTracks.get(position);
            String coverUrlLarge = track.getCoverUrlLarge();
            if (TextUtils.isEmpty(coverUrlLarge)) {
                Picasso.with(container.getContext()).load(R.mipmap.logo).into(trackPagerIv);
            }else {
                Picasso.with(container.getContext()).load(coverUrlLarge).into(trackPagerIv);
            }
        }
        return itemView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return mTracks.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    public void setTrackListData(List<Track> tracks) {
        mTracks.clear();
        mTracks.addAll(tracks);
        notifyDataSetChanged();
    }

}
